<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Controller;
use App\Http\Resources\ResumeResource;
use App\Models\Resume;
use App\Models\User;
use Illuminate\Http\Request;

class ResumeController extends ApiController
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $userId = app('userId');
        if (!$userId)  {
            return $this->errorReturn('请先登录');
        }
        $user = User::find($userId);
        if (empty($user->id)) {
            return $this->errorReturn('找不到用户');
        }
        $resume = $user->resume;
        if (!empty($resume->id)) {
            return $this->errorReturn('简历已存在');
        }
        $avatar = $request->input('avatar');
        $name = $request->input('name');
        $birth = $request->input('birth');
        $record = $request->input('record');
        $signature = $request->input('signature');
        $job_year = $request->input('job_year');
        $sex = $request->input('sex');
        $position = $request->input('position');
        $school = $request->input('school');
        $experience = $request->input('experience');
        $images = $request->input('images');

        if (!empty($avatar)) {
            $avatar = str_replace('/storage/', '', $avatar);
        }

        if (!empty($images)) {
            foreach ($images as &$img) {
                $img = str_replace('/storage/', '', $img);
            }
        }

        $resume = new Resume([
            'avatar' => $avatar,
            'name' => $name,
            'user_id' => $userId,
            'birth' => !empty($birth) ? date('Y-m-d', strtotime($birth)) : null,
            'record' => $record ?: null,
            'signature' => $signature ?: null,
            'job_year' => $job_year ?: null,
            'sex' => intval($sex),
            'position' => $position ?: null,
            'school' => $school ?: null,
            'experience' => $experience ?: null,
            'images' => $images ?: null,
        ]);

        if ($resume->save()) {
            return $this->okReturn($resume->id);
        }

        return $this->errorReturn('保存失败');

    }

    /**获取简历 */
    public function getResume(Request $request)
    {
        $userId = app('userId');
        if (!$userId)  {
            return $this->errorReturn('请先登录');
        }
        $user = User::find($userId);
        if (empty($user->id)) {
            return $this->errorReturn('找不到用户');
        }
        $resume = $user->resume;
        if (empty($resume->id)) {
            return $this->okReturn([]);
        }
        return $this->okReturn(new ResumeResource($resume));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Resume  $resume
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Resume $resume)
    {

        $avatar = $request->input('avatar');
        $name = $request->input('name');
        $birth = $request->input('birth');
        $record = $request->input('record');
        $signature = $request->input('signature');
        $job_year = $request->input('job_year');
        $sex = $request->input('sex');
        $position = $request->input('position');
        $school = $request->input('school');
        $experience = $request->input('experience');
        $images = $request->input('images');
        if (!empty($avatar)) {
            $avatar = str_replace('/storage/', '', $avatar);
        }

        if (!empty($images)) {
            foreach ($images as &$img) {
                $img = str_replace('/storage/', '', $img);
            }
        }
        $result = Resume::where('id', $resume->id)->update([
            'avatar' => $avatar,
            'name' => $name,
            'birth' => !empty($birth) ? date('Y-m-d', strtotime($birth)) : null,
            'record' => $record ?: null,
            'signature' => $signature ?: null,
            'job_year' => $job_year ?: null,
            'sex' => intval($sex),
            'position' => $position ?: null,
            'school' => $school ?: null,
            'experience' => $experience ?: null,
            'images' => $images ?: null,
        ]);
        if ($result) {
            return $this->okReturn($resume->id);
        }
        return $this->errorReturn('保存失败');
    }
}
